package com.lemon.smartsafetynew.ui.adapter.hidden;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenBo;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenProcessItemVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.TimeFormatConstants;
import com.lemon.smartsafetynew.util.hidden.SafeHiddenUtils;

import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/9/11.
 */

public class SafeHiddenProcessListAdapter extends LemonRcvAdapter<SafeHiddenProcessItemVo> {


    private RecyclerTClickListener<SafeHiddenProcessItemVo> mListener;

    public SafeHiddenProcessListAdapter(@Nullable List<SafeHiddenProcessItemVo> data,
                                        RecyclerTClickListener<SafeHiddenProcessItemVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new ItemAdapter();
    }

    public class ItemAdapter implements AdapterItem<SafeHiddenProcessItemVo> {

        private SafeHiddenProcessItemVo mData;
        private int mPosition;

        @Bind(R.id.safe_hidden_doc_picture_des_tv)
        TextView safeHiddenDocPictureDesTv;
        @Bind(R.id.safe_hidden_timestamp)
        TextView safeHiddenTimestamp;
        @Bind(R.id.safe_hidden_item_status_ImageView)
        ImageView safeHiddenItemStatusImageView;
        @Bind(R.id.white_top_line_view)
        View whiteTopLineView;
        @Bind(R.id.white_bottom_line_view)
        View whiteBottomLineView;
        @Bind(R.id.safe_hidden_person_name_tv)
        TextView safeHiddenPersonNameTv;
        @Bind(R.id.safe_hidden_item_desc_tv)
        TextView safeHiddenItemDescTv;
        @Bind(R.id.safe_hidden_choose_item_layout)
        LinearLayout safeHiddenChooseItemLayout;

        @Override
        public int getLayoutResId() {
            return R.layout.item_safe_hidden_process_record;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {

            safeHiddenChooseItemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(mData, mPosition);
                }
            });

        }

        @Override
        public void handleData(SafeHiddenProcessItemVo safeHiddenProcessItemVo, int position) {

            mData = safeHiddenProcessItemVo;
            mPosition = position;

            if (position == 0) {
                whiteTopLineView.setVisibility(View.VISIBLE);
            } else {
                whiteTopLineView.setVisibility(View.GONE);
            }

            if (SafeHiddenBo.Status.review.getValue().equals(safeHiddenProcessItemVo.getType())) {
                if(SafeHiddenBo.Result.pass.getValue().equals(safeHiddenProcessItemVo.getResult())) {
                    whiteBottomLineView.setVisibility(View.VISIBLE);
                } else if (SafeHiddenBo.Result.no_pass.getValue().equals(safeHiddenProcessItemVo.getResult())) {
                    whiteBottomLineView.setVisibility(View.GONE);
                }
            }

            safeHiddenTimestamp.setText(StringUtils.friendly_time(CommonUtil.timeToStr(new Date(safeHiddenProcessItemVo.getCreate_time()), TimeFormatConstants.FORMAT_TIME))
                    .replace(" ", "\n"));

            safeHiddenItemStatusImageView.setBackgroundResource(SafeHiddenUtils.getTypeImageRes(safeHiddenProcessItemVo.getType(), safeHiddenProcessItemVo.getResult()));

            safeHiddenPersonNameTv.setText(CommonUtil.avoidNullString(safeHiddenProcessItemVo.getOperator_name()));

            safeHiddenItemDescTv.setText(SafeHiddenUtils.getTypeStr(safeHiddenProcessItemVo.getType(), safeHiddenProcessItemVo.getResult(), safeHiddenProcessItemVo.getDocument_type()));


            if (safeHiddenProcessItemVo.getType().equals(SafeHiddenBo.Status.doc.getValue())) {

                if (safeHiddenProcessItemVo.getResult() == null) {

                    safeHiddenDocPictureDesTv.setVisibility(View.VISIBLE);
                    safeHiddenDocPictureDesTv.setText("没有上传文书图片");
                    safeHiddenDocPictureDesTv.setTextColor(Color.parseColor("#FFFF0000"));

                } else {
                    safeHiddenDocPictureDesTv.setVisibility(View.GONE);
                }

            } else {
                safeHiddenDocPictureDesTv.setVisibility(View.GONE);
            }
        }
    }
}
